package com.sport.controller;

import com.sport.model.PageParams;
import com.sport.model.Result;
import com.sport.entity.SysPermission;
import com.sport.service.SysPermissionService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * 权限表;(sys_permission)表控制层
 * @author: 开心摆烂
 * @date : 2024-11-20
 */
@RestController
@Tag(name = "菜单权限表")
@RequestMapping("/sysPermission")
@ApiSupport(author = "开心摆烂", order = 4)
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;

    @PostMapping ("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页菜单")
    public Result<Page<SysPermission>> pageQuery(@RequestBody PageParams<SysPermission> pageParams) {
        // 1.查询参数
        SysPermission params = pageParams.getParams();
        LambdaQueryWrapper<SysPermission> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysPermission::getType,params.getType());
        // 2.查询条件
        lqw.like(params.getName() != null, SysPermission::getName, params.getName());
        lqw.eq(params.getEnglishName() != null, SysPermission::getEnglishName, params.getEnglishName());
        lqw.orderByAsc(SysPermission::getSort);
        // 3.分页查询
        Page<SysPermission> page = sysPermissionService.page(pageParams.getPage(), lqw);
        if (params.isTree()){
            //4.处理查询结果，父子关系
            sysPermissionService.tree(page.getRecords());
        }
        return Result.success(page);
    }



    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody SysPermission sysPermission) {
        if (sysPermissionService.save(sysPermission)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody SysPermission sysPermission) {
        if (sysPermissionService.updateById(sysPermission)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据", parameters = {
            @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        if (sysPermissionService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }
}